package com.woniu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.dto.OrderDto;
import com.woniu.entity.Food;
import com.woniu.entity.FoodItem;
import com.woniu.entity.FoodOrder;
import com.woniu.mapper.FoodMapper;
import com.woniu.mapper.FoodOrderMapper;
import com.woniu.publishe.CustomPublisher;
import com.woniu.publishe.TopicPublisher;
import com.woniu.service.FoodOrderService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import com.woniu.util.SnowflakeIdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname FoodOrderServiceImpl
 * @Description TODO
 * @Date 2025/7/22 11:49
 * @Created by pc
 */
@Service
public class FoodOrderServiceImpl extends ServiceImpl<FoodOrderMapper, FoodOrder> implements FoodOrderService {
    @Resource
    private FoodMapper foodMapper;

    @Override
    public ResponseUtil addOrder(List<FoodItem> items) throws InterruptedException {
        // 1.先根据foodId 去 food 表查询数据 价格 折扣
        List<Integer> foodIds = items.stream().map(e -> e.getFoodId()).collect(Collectors.toList());
        List<Food> foods = foodMapper.selectBatchIds(foodIds);
        SnowflakeIdGenerator sfg = new SnowflakeIdGenerator();
        String orderId = String.valueOf(sfg.nextId());
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Food food : foods) {
            for (FoodItem item : items) {
                if(food.getId() == item.getFoodId()){
                    BeanUtils.copyProperties(food,item,"id");
                    item.setOrderId(orderId);
                    item.setFoodName(food.getName());
                    BigDecimal total = food.getPrice().multiply(
                            food.getDiscount().multiply(new BigDecimal(0.1))
                    ).multiply(new BigDecimal(item.getCount())).setScale(2, RoundingMode.HALF_UP);
                    item.setTotalPrice(total);
                    totalPrice = totalPrice.add(item.getTotalPrice());
                }
            }
        }
        // 2.添加 order表
        FoodOrder foodOrder = new FoodOrder();
        foodOrder.setOrderId(orderId);
        totalPrice = totalPrice.setScale(2,RoundingMode.HALF_UP);
        foodOrder.setTotalPrice(totalPrice);
        foodOrder.setStatus(1);
        // 3.添加item 表
        // 使用rabbitMQ异步添加
        // 将 order 和 item 封装成一个dto 一起给rabbitMQ
        OrderDto orderDto = new OrderDto(foodOrder, items);
        // 用redis 解决重复消费的问题  order:orderId
        redisTemplateInit.opsForValue().set("order:"+foodOrder.getOrderId(),foodOrder.getOrderId());
        topicPublisher.send(orderDto);  // 生成订单的
        // 延迟交换机  检查订单是否支付 查看订单的状态
        customPublisher.send(foodOrder.getOrderId(),10000);
        return ResponseUtil.get(ResponseEnum.OK,foodOrder.getOrderId());
    }
    @Resource
    private TopicPublisher topicPublisher;
    @Resource
    private CustomPublisher customPublisher;
    @Resource
    private RedisTemplate redisTemplateInit;
}
